import 'package:flutter/material.dart';
import 'package:flutterlib/common/style/quhui_style.dart';
import 'package:flutterlib/widgets/quhui_tabs.dart' as QuhuiTab;
import 'package:flutterlib/widgets/round_underline_tabindicator.dart';

///支持顶部和顶部的TabBar控件
///配合AutomaticKeepAliveClientMixin可以keep住
class QuhuiTabBarWidget extends StatefulWidget {
  final TabType type;

  final bool resizeToAvoidBottomPadding;

  final List<Widget> tabItems;

  final List<Widget> tabViews;

  final Color backgroundColor;

  final Color indicatorColor;

  final Widget title;

  final Widget leading;

  final List<Widget> actions;

  final Widget drawer;

  final Widget floatingActionButton;

  final FloatingActionButtonLocation floatingActionButtonLocation;

  final Widget bottomBar;

  final List<Widget> footerButtons;

  final int tabBarType;

  final ValueChanged<int> onPageChanged;
  final ValueChanged<int> onDoublePress;
  final ValueChanged<int> onSinglePress;
  final VoidCallback onMorePress;

  QuhuiTabBarWidget({
    Key key,
    this.type = TabType.top,
    this.tabItems,
    this.tabViews,
    this.backgroundColor,
    this.indicatorColor,
    this.title,
    this.drawer,
    this.bottomBar,
    this.onDoublePress,
    this.onSinglePress,
    this.floatingActionButtonLocation,
    this.floatingActionButton,
    this.resizeToAvoidBottomPadding = true,
    this.footerButtons,
    this.onPageChanged,
    this.leading,
    this.actions,
    this.tabBarType, this.onMorePress,
  }) : super(key: key);

  @override
  _QuhuiTabBarState createState() => new _QuhuiTabBarState();
}

class _QuhuiTabBarState extends State<QuhuiTabBarWidget>
    with SingleTickerProviderStateMixin {
  final PageController _pageController = PageController();

  TabController _tabController;

  int _index = 0;

  @override
  void initState() {
    super.initState();
    _tabController =
        new TabController(vsync: this, length: widget.tabItems.length);
  }

  ///整个页面dispose时，记得把控制器也dispose掉，释放内存
  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  _navigationPageChanged(index) {
    if (_index == index) {
      return;
    }
    _index = index;
    _tabController.animateTo(index);
    widget.onPageChanged?.call(index);
  }

  _navigationTapClick(index) {
    if (_index == index) {
      return;
    }
    _index = index;
    widget.onPageChanged?.call(index);

    ///不想要动画
    _pageController.jumpTo(MediaQuery.of(context).size.width * index);
    widget.onSinglePress?.call(index);
  }

  _navigationDoubleTapClick(index) {
    _navigationTapClick(index);
    widget.onDoublePress?.call(index);
  }

  @override
  Widget build(BuildContext context) {
    if (widget.type == TabType.top) {
      ///顶部tab bar
      return new Scaffold(
        resizeToAvoidBottomPadding: widget.resizeToAvoidBottomPadding,
        floatingActionButton:
            SafeArea(child: widget.floatingActionButton ?? Container()),
        floatingActionButtonLocation: widget.floatingActionButtonLocation,
        persistentFooterButtons: widget.footerButtons,
        appBar: new AppBar(
          backgroundColor: widget.backgroundColor,
          title: widget.title,
          centerTitle: true,
          automaticallyImplyLeading: widget.leading != null,
          leading: widget.leading == null ? null : widget.leading,
          actions: widget.actions,
          bottom: getTabBar(widget.tabBarType),
        ),
        body: new PageView(
          controller: _pageController,
          children: widget.tabViews,
          onPageChanged: _navigationPageChanged,
        ),
        bottomNavigationBar: widget.bottomBar,
      );
    }

    ///底部tab bar
    return new Scaffold(
        drawer: widget.drawer,
        appBar: new AppBar(
          backgroundColor: widget.backgroundColor,
          title: widget.title,
        ),
        body: new PageView(
          controller: _pageController,
          children: widget.tabViews,
          onPageChanged: _navigationPageChanged,
        ),
        bottomNavigationBar: new Material(
          //为了适配主题风格，包一层Material实现风格套用
          color: Theme.of(context).primaryColor, //底部导航栏主题颜色
          child: new SafeArea(
            child: new QuhuiTab.TabBar(
              //TabBar导航标签，底部导航放到Scaffold的bottomNavigationBar中
              controller: _tabController,
              //配置控制器
              tabs: widget.tabItems,
              indicatorColor: widget.indicatorColor,
              onDoubleTap: _navigationDoubleTapClick,
              onTap: _navigationTapClick, //tab标签的下划线颜色
            ),
          ),
        ));
  }

  getTabBar(int tabBarType) {
    switch (tabBarType) {
      case 1:
        return normalTabBar();
      case 2:
        return roundTabBar();
      case 3:
        return scrollTabBar();
    }
  }

  normalTabBar() {
    return TabBar(
        controller: _tabController,
        tabs: widget.tabItems,
        indicator: RoundUnderlineTabIndicator(
          insets: EdgeInsets.symmetric(horizontal: 10),
          borderSide: BorderSide(
            width: 5,
            color: QuhuiColors.mainThemeColor,
          ),
        ),
        labelStyle: TextStyle(color: QuhuiColors.mainThemeColor, fontSize: 14),
        unselectedLabelStyle:
            TextStyle(color: QuhuiColors.subTextColor, fontSize: 14),
        onTap: (index) {
          widget.onPageChanged?.call(index);
          _pageController.jumpTo(MediaQuery.of(context).size.width * index);
        });
  }

  roundTabBar() {
    return PreferredSize(
      preferredSize: const Size.fromHeight(48.0),
      child: Padding(
        padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
        child: Container(
          width: double.infinity,
          decoration: new BoxDecoration(
            color: QuhuiColors.white, // 底色
            shape: BoxShape.rectangle, // 默认值也是矩形
            borderRadius: new BorderRadius.circular((20.0)), // 圆角度
          ),
          child: new TabBar(
              isScrollable: true,
              controller: _tabController,
              tabs: widget.tabItems,
              indicator: RoundUnderlineTabIndicator(
                insets: EdgeInsets.symmetric(horizontal: 10),
                borderSide: BorderSide(
                  width: 5,
                  color: QuhuiColors.mainThemeColor,
                ),
              ),
              labelStyle:
                  TextStyle(color: QuhuiColors.mainThemeColor, fontSize: 14),
              unselectedLabelStyle:
                  TextStyle(color: QuhuiColors.subTextColor, fontSize: 14),
              onTap: (index) {
                widget.onPageChanged?.call(index);
                _pageController
                    .jumpTo(MediaQuery.of(context).size.width * index);
              }),
        ),
      ),
    );
  }

  scrollTabBar() {
    return PreferredSize(
      preferredSize: const Size.fromHeight(44.0),
      child: Stack(
          children: [

        Padding(
          padding: const EdgeInsets.fromLTRB(0, 0, 66, 0),
          child: TabBar(
              isScrollable: true,
              controller: _tabController,
              tabs: widget.tabItems,
              indicator: RoundUnderlineTabIndicator(
                insets: EdgeInsets.symmetric(horizontal: 10),
                borderSide: BorderSide(
                  width: 5,
                  color: QuhuiColors.mainThemeColor,
                ),
              ),
              labelStyle:
                  TextStyle(color: QuhuiColors.mainThemeColor, fontSize: 14),
              unselectedLabelStyle:
                  TextStyle(color: QuhuiColors.subTextColor, fontSize: 14),
              onTap: (index) {
                widget.onPageChanged?.call(index);
                _pageController.jumpTo(MediaQuery.of(context).size.width * index);
              }),
        ),
        Positioned(
          top: 0,
          right: 10,
          child: GestureDetector(
            onTap: widget.onMorePress,
            child: Container(
              width: 56,
              height: 44,

              padding: EdgeInsets.fromLTRB(12, 0, 0, 0),
              decoration: BoxDecoration(
                color: QuhuiColors.white,
                border: Border(
                  left: BorderSide(width: 1,color: QuhuiColors.subGrayColor),

                ),


              ),
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 8),
                child: Text(

                "更多",
                style: QuhuiConstant.smallText,
            ),
              ),
            ),
          ),
        )
      ]),
    );
  }
}

enum TabType { top, bottom }
